//
//  CitySelectHeader.m
//  Traveling
//
//  Created by 康苗 on 15/6/13.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "CitySelectHeader.h"

#define DescFont 15
#define LOCATION_FAILED_STR @"定位失败"

@interface CitySelectHeader()


//定位城市
@property (weak, nonatomic) IBOutlet UILabel *laLocationCityDesc;
@property (weak, nonatomic) IBOutlet UIButton *ubLocationCity;

//历史城市
@property (weak, nonatomic) IBOutlet UILabel *laHistoryCityDesc;
@property (weak, nonatomic) IBOutlet UIView *historyCityView;

//热门城市
@property (weak, nonatomic) IBOutlet UILabel *laHotCityDesc;

@property (nonatomic, copy) void(^result)(NSString *city);

@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftBtnConstraintX;

@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightBtnConstraintX;

@end

@implementation CitySelectHeader

+(instancetype)cityHeaderViewWithResult:(void (^)(NSString *))result{
    CitySelectHeader *header = [[[NSBundle mainBundle] loadNibNamed:@"CitySelectHeader" owner:nil options:nil] lastObject];
    header.result = result;
    return header;
}

-(void)awakeFromNib{
    NSString *city = [DefaultsHelper stringForKey:k_LocationAddressCity];
    if (city.length>2 && [city hasSuffix:@"市"]) {
        city = [city substringToIndex:city.length-1];
    }
    [self.ubLocationCity setTitle:ISBLANK(city)?LOCATION_FAILED_STR:city forState:UIControlStateNormal];
    
    NSString *leftString = [DefaultsHelper stringForKey:k_historyLocation1];
    NSString *rightString = [DefaultsHelper stringForKey:k_historyLocation2];
    
    if (ISBLANK(leftString)) {
        self.leftBtn.hidden = YES;
        self.rightBtn.hidden = YES;
    }else{
        [self.leftBtn setTitle:leftString forState:UIControlStateNormal];
        if (ISBLANK(rightString)) {
            self.rightBtn.hidden = YES;
            [self.leftBtnConstraintX setValue:@(1) forKey:@"multiplier"];
            [self updateConstraints];
        }else{
            [self.rightBtn setTitle:rightString forState:UIControlStateNormal];
        }
    }
}

- (IBAction)locationBtn:(UIButton *)sender {
    if (![self.ubLocationCity.currentTitle isEqualToString:LOCATION_FAILED_STR]) {
        if (self.result) {
            self.result(sender.currentTitle);
        }
    }
}

- (IBAction)historyBtnClick:(UIButton *)sender {
    if (self.result) {
        self.result(sender.currentTitle);
    }
}


@end
